﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Docking;
using DevExpress.Xpf.Docking.Base;

using UCOLOR.Helpers;
using UColor.WPF;
using UCOLOR.Model;

namespace UCOLOR.View
{
    /// <summary>
    /// HomeView.xaml 的交互逻辑
    /// </summary>
    public partial class ProductView : UCModule 
    {
        public ProductView()
        {
            InitializeComponent();
        }

        private string catalog_no;
        public ProductView(int productID)
        {
           // product = DataHelper.getProduct(productID);
            InitializeComponent();
            (Product.Content as Image).Source = product.PhotoSource;
            
            this.ProductName.Content = product.ProductName;
            this.ProductPrice.Content = product.Price.ToString();
        }




        public ProductView(string catalog_no)
        {
            InitializeComponent();
            this.catalog_no = catalog_no;
            //ShowProduct(catalog_no);
        }


        void ShowProduct(string catalog_no)
        {
            salesPart = DataHelper.GetSalesPart(catalog_no);

            (Product.Content as Image).Source = salesPart.PhotoSource;
            ProductName.Content = salesPart.catalog_desc;
            ProductPrice.Content = salesPart.cover_material;
            IList<SalesPartAssist> list = DataHelper.getSalesPartAssist(catalog_no);
            this.ProductPictures.ItemsSource = list;
           // GetCatalogProduct(catalog_no);
            ShowTemplate();
        }

        void ShowTemplate()
        {
            //TemplateView view = new TemplateView();
            //TemplateData.Children.Add(view);
            //view.SetValue(Grid.ColumnProperty, 0);
            ShowTemplateView();
        }

        /// Template类型
        /// </summary>
        /// <param name="catalog_no"></param>
        public void ShowTemplateView()
        {
            TabControl MainTab = new TabControl();
            TabItem temptb1 = new TabItem();
            temptb1.Header = "模版";
            TabItem temptb2 = new TabItem();
            RichTextBox rtb = new RichTextBox();
            temptb2.Content = rtb;
            rtb.Margin = new Thickness(0, 3, 0, 0);
            temptb2.Header = "商品详情";
            TabItem temptb3 = new TabItem();
            temptb3.Header = "用户评测";
            MainTab.Items.Add(temptb1);
            MainTab.Items.Add(temptb2);
            MainTab.Items.Add(temptb3);
            MainTab.Margin = new Thickness(0, 3, 0, 0);

           // IList<SalesCatalogTemplate> cTemplates = DataHelper.GetSalesCatalogTemplate(catalog_no);

            IList<SalesTemplateType> list = DataHelper.GetSalesTemplateType(catalog_no);
            TabControl tab;
            tab = new TabControl();
            tab.Uid = "tabTemplate";

            TabItem _temptb = new TabItem();
            _temptb.Header = "所有";
         //  
            string allwhere = "";
            foreach (SalesTemplateType part in list)
            {
                TabItem temptb = new TabItem();
                temptb.Header = part.description;
                tab.Items.Add(temptb);
                //tab.SelectedIndex = tab.Items.Count - 1;
                TemplateListView templateListView = new TemplateListView(part.template_type);
                
                if (string.IsNullOrEmpty(allwhere))
                    allwhere = "('" + part.template_type + "'";
                else
                    allwhere = allwhere + ",'" + part.template_type + "'";
                templateListView.ParentModulePage = this.ParentModulePage;
                templateListView.DataContext = this;
                temptb.Content = templateListView;
                temptb.Padding = new Thickness(2, 2, 2, 2);
            }
            if (!string.IsNullOrEmpty(allwhere))
                allwhere = "template_type in " + allwhere + ")";
            tab.Items.Insert(0,_temptb);
            //tab.SelectedIndex = tab.Items.Count - 1;
            TemplateListView _templateListView = new TemplateListView(allwhere,true);
            
            _templateListView.ParentModulePage = this.ParentModulePage;
            _templateListView.DataContext = this;
            _temptb.Content = _templateListView;
            _temptb.Padding = new Thickness(2, 2, 2, 2);
            this.TemplateData.ColumnDefinitions.Clear();
            int cCount = this.TemplateData.Children.Count;
            for (int i = 0; i < cCount; i++)
            {
                this.TemplateData.Children.RemoveAt(0);
            }
            //this.ViewList.Add(Key, view);
            tab.Margin = new Thickness(0, 3, 0, 0);
            temptb1.Content = tab;
            this.TemplateData.Children.Add(MainTab);
            MainTab.SetValue(Grid.ColumnProperty, 0);
        }


        public void ShowProduct(int productID)
        {
            // product = DataHelper.getProduct(productID);
            //InitializeComponent();
            //(Product.Content as Image).Source = product.PhotoSource;
            //ProductName.Content = product.ProductName;
            //ProductPrice.Content = product.Price;
           // this.ProductPictures.ItemsSource = UCOLOR.Model.DataHelper.getProductPictures(productID);
            ShowProduct(catalog_no);
        }
        
        private void GetCatalogProduct(string catalog_no)
        {
            IList<ExhibitionGroupItem> itemlist = DataHelper.GetExhibitionGroupItemByCatalog(catalog_no);
            if (itemlist != null)
            {
                itemlist = DataHelper.GetExhibitionGroupItem( itemlist[0].group_id);
            }
           // this.DirList.ItemsSource = itemlist;
            //string groupid = DataHelper.(catalog_no).Where(p=>p.catalog_no==catalog_no).First().group_id;

        }
        public SalesPart salesPart
        {
            get;
            set;
        }

        public Product product
        {
            get;set;
        }



        public IList<Product> data2
        {
            get;
            set;
        }

        
        public IList<Product> data
        {
            get;
            set;
        }

        private void ShowModalDialog(bool bShow)
        {
         //   this.ModalDialog.IsOpen = bShow;
            //this.MainPanel.IsEnabled = !bShow;
        }

        private void Dlg_BtnClose_Click(object sender, RoutedEventArgs e)
        {
            ShowModalDialog(false);
        }


        private void Dlg_BtnOK_Click(object sender, RoutedEventArgs e)
        {
            ShowModalDialog(false);
            //labelString.Content = "The string is: " + TxtBoxInput.Text;
        }  


        private void Tile_Click(object sender, EventArgs e)
        {
            //var name = ((DevExpress.Xpf.LayoutControl.Tile)sender).Name;
            if (((DevExpress.Xpf.LayoutControl.Tile)sender).Content is UCOLOR.Model.SalesPartAssist)
            {
                (Product.Content as Image).Source = (((DevExpress.Xpf.LayoutControl.Tile)sender).Content as UCOLOR.Model.SalesPartAssist).PhotoSource;
            }
            else 
            {
                ShowProduct(((ExhibitionGroupItem)(((DevExpress.Xpf.LayoutControl.Tile)sender).Content)).catalog_no);
            }
                //ShowDir(((DevExpress.Xpf.LayoutControl.Tile)sender).Uid);
        }
    }
}
